package com.autumn.platform.auth.core.authc.impl;

import java.util.Collection;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.autumn.platform.auth.core.access.IPermissionProvider;
import com.autumn.platform.auth.core.authc.IAuthenticationListener;
import com.autumn.platform.auth.core.authc.IAuthenticationToken;
import com.autumn.platform.core.context.user.ISessionUser;

public class PermissionProviderListener implements IAuthenticationListener {

    @Autowired(required = false)
    private List<IPermissionProvider> providers;

    @Override
    public void onSuccess(IAuthenticationToken authenticationToken, AuthenticationContext context) {
        ISessionUser user = context.getSessionUser();
        if (null != user && null != providers) {
            for (IPermissionProvider provider : providers) {
                Collection<String> ids = provider.getPermissionIds(user);
                if (null != ids) {
                    user.getPermissionIds().addAll(ids);
                }
            }
        }
    }

}
